<template>
  <div class="edit-container">
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" />

    <div class="avater">
      <van-image
        width="120"
        height="120"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        :value="userInfo.name"
        is-link
        @click="clickName"
      />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="
          showDatePicker = true
          currentDate = new Date(userInfo.birthday)
        "
      />
      <van-cell title="选择单个日期" :value="date" @click="show = true" />
      <van-calendar v-model="show" @confirm="onConfirm" />
    </van-cell-group>

    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      @confirm="editName"
      show-cancel-button
    >
      <van-field v-model="newName" ref="input" placeholder="请输入姓名" />
    </van-dialog>

    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="editGender(0)" />
      <van-cell title="女" is-link @click="editGender(1)" />
    </van-popup>

    <!-- 时间插件 -->
    <van-popup v-model="showDatePicker" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePicker = false"
        @confirm="editBrithday"
      />
    </van-popup>

    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="120"
        autoCropHeight="120"
      ></vueCropper>
      <van-button class="cancel" @click="showCropper = false">取消</van-button>
      <van-button type="info" class="confirm" @click="editPhotoClick"
        >确认</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, editPhoto } from './user.js'
import * as moment from 'moment'
import { VueCropper } from 'vue-cropper'

export default {
  name: 'edit',
  data () {
    return {
      showDialog: false,
      newName: '',
      showPopup: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      showDatePicker: false,
      date: '',
      show: false,
      img: '',
      showCropper: false
    }
  },
  components: {
    VueCropper
  },
  methods: {
    clickName () {
      this.showDialog = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    editName () {
      editUserInfo({ name: this.newName }).then(res => {
        this.$toast.success('修改姓名成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          name: this.newName
        })
      })
    },
    editGender (gender) {
      editUserInfo({ gender: gender }).then(res => {
        console.log(res)
        this.showPopup = false
        this.$toast.success('修改性别成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          gender
        })
      })
    },
    // 更改生日信息
    editBrithday () {
      console.log(this.currentDate)
      let formaDate = moment(this.currentDate).format('YYYY-MM-DD')
      console.log(formaDate)
      editUserInfo({ birthday: formaDate }).then(res => {
        console.log(res)
        this.$toast.success('修改时间成功')
        this.$store.commit('updateUserInfo', {
          ...this.userInfo,
          birthday: formaDate
        })
        this.showDatePicker = false
      })
    },

    afterRead (file) {
      // // 此时可以自行将文件上传至服务器
      // console.log(file)
      // let fd = new FormData()
      // fd.append('photo', file.file)
      // console.log(fd)
      // editPhoto(fd).then(res => {
      //   console.log(res)
      //   this.$toast.success('更新头像成功')
      //   this.$store.commit('updateUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
      this.img = file.content
      this.showCropper = true
    },

    formatDate (date) {
      return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    },
    onConfirm (date) {
      this.show = false
      this.date = this.formatDate(date)
    },
    editPhotoClick () {
      this.$refs.cropper.getCropBlob(data => {
        // do something
        console.log(data)
        let fd = new FormData()
        fd.append('photo', data)
        console.log(fd)
        editPhoto(fd).then(res => {
          console.log(res)
          this.$toast.success('更新头像成功')
          this.$store.commit('updateUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          });
          this.showCropper=false
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.edit-container {
  .avater {
    height: 150px;
    display: flex;
    justify-content: center;
    align-items: center;
    .van-uploader {
      position: absolute;
      width: 120px;
      height: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
}
.cropper-box {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  .cancel {
    width: 50%;
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .confirm {
    position: absolute;
    width: 50%;
    right: 0;
    bottom: 0;
  }
}
</style>
